package com.libraryinformation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.libraryinformation.dao.BacklogDao;
import com.libraryinformation.domain.Backlog;
import com.libraryinformation.domain.BacklogVO;
import com.libraryinformation.domain.Weather;
import com.libraryinformation.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@RestController
@Transactional
@RequestMapping("/backlog")
public class BacklogController {
@Autowired
private BacklogDao  backlogDao;
    /**
     * 新增或更新待办事项
     *
     * @param backlog
     * @return
     */
    @PostMapping("/insertOrUpdate")
    public Result insertOrUpdateWeather(@RequestBody Backlog backlog) {
        backlog.setInsertTime(TimeUtil.convertDateToString());
        int flag = backlogDao.insertOrUpdateWeather(backlog);
        Integer code = flag == 1 ? Code.SAVE_OK : Code.SAVE_ERR;
        String msg = flag == 1 ? "操作成功" : "操作失败，请重试！";
        return new Result(code, flag, msg);
    }
    /**
     * 更新代办事项是否已检查
     *
     * @param backlog
     * @return
     */
    @PutMapping("/updateStatus")
    public Result update(@RequestBody Backlog backlog) {
        int flag = backlogDao.updateByStatus(backlog.getStatus(), backlog.getId());
        return new Result(flag == 1 ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /**
     * 删除借阅信息,即更新deleted状态
     *
     * @param id
     * @return
     */
    @PutMapping("/updateDeleted/{id}")
    public Result delete(@PathVariable Long id) {
        int flag = backlogDao.updateByDeleted(id);
        return new Result(flag == 1 ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    /**
     * 根据id查询待办事项
     *
     * @param id
     * @return
     */
    @GetMapping("/selectOne/{id}")
    public Result getById(@PathVariable String id) {
        Backlog backlog = backlogDao.selectById(id);
        Integer code = backlog != null ? Code.GET_OK : Code.GET_ERR;
        String msg = backlog != null ? "查询成功" : "数据查询失败，请重试！";
        return new Result(code, backlog, msg);
    }

    /**
     * 查询所有待办事项
     *
     * @return
     */
    @GetMapping("/selectAll")
    public Result getAll() {
        List<BacklogVO> backlogList = backlogDao.getAll();
        Integer code = backlogList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = backlogList != null ? "" : "数据查询失败，请重试！";
        return new Result(code, backlogList, msg);
    }
    /**
     * 分页查询待办事项
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/selectAllByPage")
    public Result getByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        IPage<Backlog> backlogPage = new Page<>(page, size);
        IPage<BacklogVO> backlogVOPage = backlogDao.selectAllPage(backlogPage);
        Integer code = backlogVOPage != null ? Code.GET_OK : Code.GET_ERR;
        String msg = backlogVOPage != null ? "" : "数据查询失败，请重试！";
        return new Result(code, backlogVOPage, msg);
    }
}
